//
//  ViewController.m
//  HelloTaxi
//
//  Created by zhenhua zhou on 12-7-9.
//  Copyright (c) 2012年 ECUST. All rights reserved.
//

#import "ViewController.h"
#import "GeoInfo.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize priceLabel = _priceLabel;
@synthesize distanceLabel = _distanceLabel;
@synthesize durationLabel = _durationLabel;
@synthesize startTextField = _startTextField;
@synthesize endTextField = _endTextField;
//@synthesize mapView = _mapView;
@synthesize mapView = _mapView;
@synthesize mapDelegate = _mapDelegate;
//@synthesize startBoxDelegate = _startBoxDelegate;
@synthesize locationManager;

@synthesize latLabel = _latLabel;
@synthesize lonLabel = _lonLabel;

@synthesize scorllerUp = _scorllerUp;
@synthesize scorllerDown = _scorllerDown;

- (void)viewDidLoad
{
    [super viewDidLoad];
    _mapDelegate.MapView = _mapView;

	// Do any additional setup after loading the view, typically from a nib.
    if (locationManager == nil) {
        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
        [locationManager setDelegate:self];
    }
    [self.locationManager startUpdatingLocation];
    
    
    
  //  [self closeMap];
    mIsMapOpened = false;
    _mapMaxHeight = 283;
    
    [[GeoInfo getInstance] setPriceLabel:self.priceLabel];
    [[GeoInfo getInstance] setDistanceLabel:self.distanceLabel];
    [[GeoInfo getInstance] setDurationLabel:self.durationLabel];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    [locationManager stopUpdatingLocation];
    [locationManager release];
    locationManager = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(void)startRouting;
{
    [[GeoInfo getInstance] setMagiMapView:_mapDelegate];
    
    if(self.startTextField.text.length >0)
    {
        [[GeoInfo getInstance] searchRouteFrom:[self.startTextField text] To:[self.endTextField text]];
    }
    else {
        
        CLLocation *location = [locationManager location];
        if (location != nil) {
            CLLocationCoordinate2D coordinate = location.coordinate;
            
            _latLabel.text =  [NSString stringWithFormat:@"%f",coordinate.latitude];
            _lonLabel.text =  [NSString stringWithFormat:@"%f",coordinate.longitude];
            
            [[GeoInfo getInstance] searchRouteFromPos:&coordinate To:[self.endTextField text]];
        }
        
        
    }
}

-(IBAction)testAction:(id)sender
{
  
  //  [[GeoInfo getInstance] testFunc:self.testTextField];
    [self startRouting];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == _startTextField)
    {
        [textField resignFirstResponder];
        [self.endTextField becomeFirstResponder];
    }
    else if(textField == _endTextField)
    {
        [textField resignFirstResponder];
        [self startRouting];
    }
    return YES;
}

-(IBAction)switchMapState:(id)sender
{
    if(mIsMapOpened)
    {
        [self closeMap];
    }
    else
    {
        [self openMap];
    }
}

-(IBAction)backgroundTap:(id)sender
{
    [_endTextField resignFirstResponder];
    [_startTextField resignFirstResponder];
}

- (void)openMap
{
    mIsMapOpened = true;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    self.mapView.frame = CGRectMake(self.mapView.frame.origin.x, self.mapView.frame.origin.y, self.mapView.frame.size.width, self.mapView.frame.size.height+_mapMaxHeight);
    self.scorllerDown.frame = CGRectMake(self.scorllerDown.frame.origin.x, self.scorllerDown.frame.origin.y+ _mapMaxHeight, self.scorllerDown.frame.size.width, self.scorllerDown.frame.size.height);
    [UIView commitAnimations];
    [[GeoInfo getInstance] showRoute:_mapDelegate];
}

-(void)closeMap
{
    mIsMapOpened = false;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    self.mapView.frame = CGRectMake(self.mapView.frame.origin.x, self.mapView.frame.origin.y, self.mapView.frame.size.width, self.mapView.frame.size.height-_mapMaxHeight);
    self.scorllerDown.frame = CGRectMake(self.scorllerDown.frame.origin.x, self.scorllerDown.frame.origin.y- _mapMaxHeight, self.scorllerDown.frame.size.width, self.scorllerDown.frame.size.height);
    [UIView commitAnimations];

}


#pragma mark -
#pragma mark locationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    if (newLocation.horizontalAccuracy < 10 && newLocation.verticalAccuracy < 10) {
        currentLocation = newLocation;
        [locationManager stopUpdatingLocation];
    }
    
}

@end
